package com.adminx.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adminx.dao.MessageDao;
import com.adminx.dao.MessageDetailDao;
import com.adminx.entity.Message;
import com.adminx.util.PageResults;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class MessageService {
	
	
	@Autowired
	private MessageDao messageDao;
	
	@Autowired
	private MessageDetailDao messageDetailDao;
	
	/**
	 * 分页查找消息
	 */
	public PageResults<Message> query(Integer pageIndex, Integer pageSize){
		return this.messageDao.query(pageIndex, pageSize);
	}
	
	/**
	 * 批量删除
	 */
	public int delBatch(String ids){
		return this.messageDao.delBatch(ids);
	}
	
	/**
	 * 通过ID查询message
	 */
	public Message info(Long codeId) {
		String hql = "From Message t where t.messageId = ?";
		return this.messageDao.getByHQL(hql,codeId);
	}
	
	/**
	 * 新增/修改值集
	 * @param code
	 */
	public void save(Message message){
		//新增
		if(message.getMessageId() == null){
			message.setStatus(1);
			this.messageDao.save(message);
		}
		//修改
		else{
			Message oldMessage = this.messageDao.get(message.getMessageId());
			oldMessage.setTitle(message.getTitle());
			oldMessage.setContent(message.getContent());
			oldMessage.setUpdateDate(new Date());
			oldMessage.setCreateDate(message.getCreateDate());
			this.messageDao.update(oldMessage);
		}
	}
	
	/**
	 * 推�?消息
	 */
	public void push(String accountIds, Long messageId){
		this.messageDetailDao.save(accountIds, messageId);
	}
}
